from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ProfilePage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.first_name_input_locator = (By.ID, "first-name")
        self.last_name_input_locator = (By.ID, "last-name")
        self.email_input_locator = (By.ID, "email")
        self.save_button_locator = (By.ID, "save-button")
        self.error_message_locator = (By.ID, "error-message")
        self.success_message_locator = (By.ID, "success-message")

    def navigate(self):
        self.driver.get('http://example.com/profile')

    def enter_first_name(self, first_name):
        first_name_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.first_name_input_locator)
        )
        first_name_input.clear()
        first_name_input.send_keys(first_name)

    def enter_last_name(self, last_name):
        last_name_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.last_name_input_locator)
        )
        last_name_input.clear()
        last_name_input.send_keys(last_name)

    def enter_email(self, email):
        email_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.email_input_locator)
        )
        email_input.clear()
        email_input.send_keys(email)

    def click_save_button(self):
        save_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.save_button_locator)
        )
        save_button.click()

    def get_error_message(self):
        error_message = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.error_message_locator)
        )
        return error_message.text

    def get_success_message(self):
        success_message = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.success_message_locator)
        )
        return success_message.text

    def is_save_button_enabled(self):
        save_button = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.save_button_locator)
        )
        return save_button.is_enabled()